package com.newtouch.xcd.common.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Validator;
import com.newtouch.xcd.common.utils.vo.TimeRangeItem;
import com.newtouch.xcd.common.utils.vo.TimeRangeItemComparator;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 校验类
 */
public class ValidatorUtils{


    /**
     * 校验是否是时间字段
     * @param time HH:ss
     * @return
     */
    public static Boolean isTime(String time){
        return Validator.isMactchRegex("((1|0?)[0-9]|2[0-3]):([0-5][0-9])",time);
    }

    /**
     * 判断时间点列表是否有重复
     * @param timePoints 时间点
     * true 有重复，false没有重复
     * @return
     */
    public static Boolean checkTimePoint(List<String> timePoints){

        Set<String> set = new HashSet<>();
        for(String timePoint:timePoints){
            if(set.contains(timePoint)){
                return true;
            }else{
                set.add(timePoint);
            }
        }

        return false;
    }

    /**
     * 判断时间范围，不允许重叠
     * @param timeRangeItems 时间点
     * true 有问题，false没有问题
     * @return
     */
    public static Boolean checkTimeRange(List<TimeRangeItem> timeRangeItems){

        //先排序
        CollectionUtil.sort(timeRangeItems,new TimeRangeItemComparator());

        List<String> list = new ArrayList<>();
        //将排好序的时间段依次放入list中
        String maxTime = "";
        for(TimeRangeItem timeRangeItem:timeRangeItems){
            String startTime = timeRangeItem.getStartTime();

            if(startTime.compareTo(maxTime)>0){
                list.add(startTime);
                maxTime = startTime;
            }else{
                return true;
            }


            String endTime = timeRangeItem.getEndTime();

            if(endTime.compareTo(maxTime)>0){
                list.add(endTime);
                maxTime = endTime;
            }else{
                return true;
            }
        }

        return false;

    }

}




